package com.hustzp.com.xichuangzhu.poetry;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.fragment.CategoryFragment;
import com.hustzp.com.xichuangzhu.model.Collections;
import com.hustzp.com.xichuangzhu.poetry.adapter.CollectionWorkAdapter;
import com.hustzp.com.xichuangzhu.poetry.dao.CollectionKindListDao;
import com.hustzp.com.xichuangzhu.poetry.model.CollectionWorks;
import com.hustzp.com.xichuangzhu.poetry.model.Works;
import com.hustzp.com.xichuangzhu.utils.ImageUtils;
import com.hustzp.com.xichuangzhu.widget.CircleImageView;

import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/28 21:26
 * @Description:分类作品列表页
 */
public class CatagoryListAct extends FragmentActivity implements AbsListView.OnScrollListener, AdapterView.OnItemClickListener {
    private ListView lvList;
    private TextView tvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_list);
        lvList = (ListView) findViewById(R.id.eLvKindsList);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        TextView tvBack = (TextView) findViewById(R.id.tv_back);
        String from = getIntent().getStringExtra("from");
        if (CategoryFragment.class.getSimpleName().equals(from)) {
            tvBack.setText("分类");
        } else {
            tvBack.setText("返回");
        }
        lvList.setOnScrollListener(this);
        lvList.setOnItemClickListener(this);
        loadData((Collections) getIntent().getSerializableExtra(Collections.class.getSimpleName()));
    }

    private void addHeader(Collections collections) {
        View headerView = LayoutInflater.from(this).inflate(R.layout.activity_category_list_header, null);
        CircleImageView img_cover = (CircleImageView) headerView.findViewById(R.id.img_cover);
        TextView tv_name = (TextView) headerView.findViewById(R.id.tv_name);
        TextView tv_desc = (TextView) headerView.findViewById(R.id.tv_desc);
        ImageUtils.loadImage(collections.getCover(), img_cover);
        tv_name.setText(collections.getName());
        tv_desc.setText(collections.getDesc());

        tvTitle.setText(collections.getName());
        lvList.addHeaderView(headerView);
    }

    private void loadData(Collections collections) {
        addHeader(collections);
        CollectionKindListDao dao = new CollectionKindListDao(this);
        List<CollectionWorks> collectionKindses = dao.getCollectioinWorks(collections);
        BaseAdapter adapter = new CollectionWorkAdapter(this, collectionKindses);
        lvList.setAdapter(adapter);
    }

    public void onClose(View v) {
        finish();
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    private void startAnim(final boolean show) {
        if (tvTitle.getVisibility() == View.VISIBLE && show) {
            return;
        } else if (tvTitle.getVisibility() == View.GONE && !show) {
            return;
        }
        float from = 0.0f;
        float to = 0.0f;
        if (show) {
            //从全透明变为全不透明
            to = 1.0f;
        } else {
            //从全不透明变为全透明
            from = 1.0f;
        }
        AlphaAnimation aAnima = new AlphaAnimation(from, to);
        aAnima.setDuration(200);
        tvTitle.startAnimation(aAnima);
        aAnima.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                tvTitle.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tvTitle.setVisibility(show ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        startAnim(firstVisibleItem > 1);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            return;
        }
        CollectionWorks works = (CollectionWorks) parent.getAdapter().getItem(position);
        Intent intent = new Intent(this, PoetryDetailAct.class);
        Works work = new Works();
        work.setId(works.getWork_id());
        intent.putExtra(Works.class.getSimpleName(), work);
        startActivity(intent);
    }
}
